package com.lwd.util;

import java.io.Serializable;

public class Tuple<A, B> implements Serializable {

    public final A a;
    public final B b;

    public Tuple(A a, B b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int hashCode() {
        return (a == null ? 0 : a.hashCode()) * 31 + (b == null ? 0 : b.hashCode());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Tuple)) {
            return false;
        }

        Tuple other = (Tuple) obj;
        if (equals(this.a, other.a) && equals(this.b, other.b)) {
            return true;
        }

        return false;
    }

    private boolean equals(Object obj1, Object obj2) {
        return obj1 == obj2 || (obj1 != null && obj1.equals(obj2));
    }

    @Override
    public String toString() {
        return String.format("{a=%s, b=%s}", a, b);
    }
}